<?php
/**
 * lertaLight - A smart ContentManagementSystem
 * --
 * 
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * --
 *
 * @package 	lertaLight
 * @author 		Frederic Darmstädter <fredericdarmstaedter@gmail.com>
 * @copyright 	2010 Frederic Darmstädter <fredericdarmstaedter@gmail.com>
 * @license 	http://opensource.org/licenses/mit-license.php MIT License
 * @version 	2.0 (2010-01-16)
 */
	class page{

		public function getPage($requestedPage){
			if($this->checkSystemStatus()){
				$config = new config();
				$routes = $config->routes();
				$configuration = $config->config();
				$template = $config->templates();
				
				// Convert requestedPage
				$requestedPage = str_replace($configuration['baseUrl'],'/', $requestedPage);
				
				// If redirect is enabled for this entry
				if(! empty($routes[$requestedPage]['redirect'])){
					header('location: ' . $configuration['baseUrl'].$routes[$requestedPage]['redirect']);
					exit;
				}
				// If site is not configured
				if(empty($routes[$requestedPage]))
					$requestedPage = '404';
				
				// Get Templatename
				if(! empty($routes[$requestedPage]['template']))
					$templateName = $routes[$requestedPage]['template'];
				else
					$templateName = $configuration['standard_template'];
				
				// Get Templatefolder and file
				$templateFolder = $template[$templateName]['folder'];
				$templateFile = $template[$templateName]['file'];
								
				// Load Template
				$output = $this->getTemplate('lertaLight/template/'. $templateFolder .'/'. $templateFile);
				
				// Set marker content
				$output = $this->setMarkerContent($output, $requestedPage);
				
				// Correct Filenames
				$output = $this->correctFilenames($output, $templateFolder);
				
			}
			else{
				$error = new error();
				$output = $error->setupLertaLight();
			}

			return $output;
		}

		protected function getTemplate($templateFile){
			$file = fopen($templateFile, 'r');
			$templateContent = file_get_contents($templateFile);
			fclose($file);

			return $templateContent;
		}

		protected function setMarkerContent($page, $requestedPage){

			$config = new config();
			$routes = $config->routes();

			$templates = $config->templates();

			$menu = new menuGenerator();

			$pageTitle = $routes[$requestedPage]['title'];
			$menuContent = $menu->getMenu($requestedPage, $routes);

			$filename = $routes[$requestedPage]['file'];
			$file = new file();
			$pageContent = $file->getContentOfFile('lertaLight/pages/'.$filename);

			$page = $this->exchangeMarkerContent('###TITLE###', $pageTitle, $page);
			$page = $this->exchangeMarkerContent('###MENU###', $menuContent, $page);
			$page = $this->exchangeMarkerContent('###CONTENT###', $pageContent, $page);

			# local markers
			if(! empty($routes[$requestedPage]['marker']))
				$page = $this->setCustomMakerContent($page, $requestedPage, $routes[$requestedPage]['marker'], $routes);


			$template = 'mytemplate';
			
			# global markers
			if(! empty($templates[$template]['marker']))
				$page = $this->setCustomMakerContent($page, $template, $templates[$template]['marker'], $templates);

			return $page;
		}

		protected function setCustomMakerContent($page, $requestedPage, $markers, $routes){

			if(! empty($markers)){
				$i = 0;
				foreach($markers as $marker){
					$contentFile = 'lertaLight/pages/' . $marker;
					// load content
					if(file_exists($contentFile)){
						$file = fopen($contentFile, 'r');
						$include = file_get_contents($contentFile);
						fclose($file);
					}
					else{
						$include = 'This page is not here!';
					}
					$markerNames = array_keys($routes[$requestedPage]['marker']);
					$markerName = '###' . $markerNames[$i] . '###';
					$page = str_replace($markerName, $include, $page);
					$i++;
				}
			}

			return $page;
		}

		protected function exchangeMarkerContent($marker,$markerContent,$page){
			return str_replace($marker, $markerContent, $page);
		}

		protected function checkSystemStatus(){
			if(! file_exists('lertaLight/conf/configuration.yaml')){
				$error = new error();
				print $error->lertaLightFileMissing('lertaLight/conf/configuration.yaml');
				return false;
			}


			return true;
		}
		
		protected function correctFilenames($page, $templateFolder){
			// CORRECT FILENAMES
			$output = str_replace('library/', 'library/'.$templateFolder.'/', $page);
			
			// Add Base
			$config = new config();
			$configuration = $config->config();
			$baseurl = $configuration['baseUrl'];
			$base = '</title>	
		<base href="'. $baseurl .'" />';
			$output = str_replace('</title>',$base,$output);
			
			return $output;
		}
	}